package com.industrial.premu.controller.chart;

import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.service.IPreMuBenefitEvaluationService;
import com.industrial.premu.util.PageUtils;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * 行业指标分析
 */
@RestController
@RequestMapping("/chart/industryAnalysis")
public class PreMuIndustryAnalysisController extends BaseController {

    @Autowired
    private IPreMuBenefitEvaluationService preMuBenefitEvaluationService;

    @GetMapping(value = "/getIndustry")
    public AjaxResult getIndustry(PreMuChartsDto preChartsDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
        }
        String  indexName=preChartsDto.getIndexName();
        switch (indexName) {
            case "单位用地税收": preChartsDto.setIndexName("dwydss");
                break;
            case "单位能耗销售收入":preChartsDto.setIndexName("dwnhxs");
                break;
                //2025年改名了
            case "单位能耗营业收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位污染物排放销售收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "单位污染物排放营业收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "研发经费投入强度":preChartsDto.setIndexName("yfjftr");
                break;
            case "全员劳动生产率":preChartsDto.setIndexName("qyldscl");
                break;
            case "单位用地销售收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "单位用地营业收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "收入利润率" : preChartsDto.setIndexName("srlrl");
                break;
            default:
                logger.info("无此选项!");
        }
        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.groupIndexByIndustry(preChartsDto);

        return  success(PageUtils.paginateList(chartsVoList,preChartsDto.getPageNum(),preChartsDto.getPageSize()));
    }
    @GetMapping(value = "/getAvgIndustry")
    public AjaxResult getAvgIndustry(PreMuChartsDto preChartsDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
        }
        String  indexName=preChartsDto.getIndexName();
        switch (indexName) {
            case "单位用地税收": preChartsDto.setIndexName("dwydss");

                break;
            case "单位能耗销售收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位能耗营业收入":preChartsDto.setIndexName("dwnhxs");
                break;
            case "单位污染物排放销售收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "单位污染物排放营业收入":preChartsDto.setIndexName("dwwrwpf");
                break;
            case "研发经费投入强度":preChartsDto.setIndexName("yfjftr");
                break;
            case "全员劳动生产率":preChartsDto.setIndexName("qyldscl");
                break;
            case "单位用地销售收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "单位用地营业收入":preChartsDto.setIndexName("dwydxs");
                break;
            case "收入利润率" : preChartsDto.setIndexName("srlrl");
                break;
            default:
                logger.info("无此选项!");
        }
        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.groupIndexByIndustry(preChartsDto);
        BigDecimal sum = null;
      if(chartsVoList.size()!=0){
          if(chartsVoList!=null&&!chartsVoList.isEmpty()){
              sum = chartsVoList.stream().map(i -> new BigDecimal(i.getIndexNum())).reduce(BigDecimal::add).get();
          }
          BigDecimal size=new BigDecimal(chartsVoList.size());
          BigDecimal avg=sum.divide(size,2,BigDecimal.ROUND_HALF_UP);
          for (int i = 0; i < chartsVoList.size(); i++) {
              BigDecimal indexNum=new BigDecimal(chartsVoList.get(i).getIndexNum());
              chartsVoList.get(i).setAvgNum(String.valueOf(indexNum.subtract(avg)));
          }
      }
        return  success(chartsVoList);
    }

}
